############################################周星宏############################################
from django.db.models.query import QuerySet
from django.http import Http404
from django_redis import get_redis_connection
from rest_framework.generics import ListAPIView, CreateAPIView, GenericAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from spits.models import Spit
from spits.serializers import SpitAllSerializer

# 获取和增加吐槽
class SpitsAllView(ListAPIView,CreateAPIView):
    queryset = Spit.objects.all()
    serializer_class = SpitAllSerializer
    def get_queryset(self):
        queryset = self.queryset
        if isinstance(queryset, QuerySet):
            # Ensure queryset is re-evaluated on each request.
            queryset = queryset.filter(parent_id=None)
        return queryset
    def get(self, request, *args, **kwargs):
        spit = self.get_queryset()
        serializer = self.get_serializer(spit,many=True, )
        return Response(serializer.data, status=201)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data, context={"request": request})
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

# 修改、取消吐槽收藏
class SpitsCollectedView(APIView):
    def put(self,request,id):
        redis_coon = get_redis_connection('myspit')
        collected = redis_coon.smembers(f'split_collection_{request.user.id}')

        if id in collected:
            redis_coon.srem(f'split_collection_{request.user.id}',id)
        else:
            redis_coon.sadd(f'split_collection_{request.user.id}',id)
        return Response({
            'success': True,
            'message': '收藏成功'
        })
    pagination_class =None

# 点赞
class SpitLikeView(APIView):
    def put(self,request,id):
        redis_coon = get_redis_connection('myspit')
        good = redis_coon.smembers(f'good_{request.user.id}')
        spit = Spit.objects.get(id=id)
        if id in good:
            redis_coon.srem(f'good_{request.user.id}',id)
            spit.thumbup -= 1
        else:
            redis_coon.sadd(f'good_{request.user.id}',id)
            spit.thumbup += 1
        spit.save()


        return Response({
            'success': True,
            'message': '点赞成功'
        })

    pagination_class = None

# 吐槽详情
class SpitContentView(GenericAPIView):
    serializer_class = SpitAllSerializer
    def get(self,request,id):
        try:
            spit= Spit.objects.get(id=id)
            spit.visits += 1
            spit.save()
        except:
            raise Http404
        serializer = self.get_serializer(spit)
        return Response(serializer.data)
    pagination_class = None

# 吐槽
class SpitChildrenView(GenericAPIView):
    serializer_class = SpitAllSerializer
    def get(self,requets,id):
        try:
            spit = Spit.objects.filter(parent_id=id)
        except:
            raise Http404
        serializer = self.get_serializer(spit,many=True)
        return Response(serializer.data)
    pagination_class = None

############################################周星宏############################################